package org.vm.cmbcdirect.component.service;

import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.vm.cmbcdirect.component.dao.FaceFacesetDao;
import org.vm.cmbcdirect.component.dao.FacePersonDao;
import org.vm.cmbcdirect.component.model.FaceFaceset;
import org.vm.cmbcdirect.component.model.FacePerson;
import org.vm.cmbcdirect.component.utils.StringRoleUtil;
import org.vm.cmbcdirect.utils.BaseUtil;
import org.vm.cmbcdirect.utils.DateUtil;
import org.vm.cmbcdirect.utils.HttpUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by violorch on 2016/3/25.
 */
public class ManagerFacePic {

    private final static Logger LOGGER = Logger.getLogger(ManagerFacePic.class);

    @Autowired
    private FacePersonDao facePersonDao;

    @Autowired
    private FaceFacesetDao faceFacesetDao;

    /**
     * 新建Person
     * @param personName
     * @param faceId
     * @param tag
     * @param groupId
     * @param groupName
     */
    private final static String FACE_CREATE_PERSON_URL = BaseUtil.getFaceApiUrl() + "/person/create";
    public boolean createPerson(String personName, String[] faceId, String tag, String groupId, String groupName){

        List<String> params = new ArrayList<>();

        if (personName != null) params.add("person_name=" + personName);
        if (faceId != null) params.add("face_id=" + StringRoleUtil.addArray(faceId));
        if (tag != null)    params.add("tag=" + tag);
        if (groupId != null) {
            params.add("group_id=" + groupId);
        } else if (groupName != null) {
            params.add("group_name=" +  groupId);
        }

        try {
            JSONObject resultJson = HttpUtil.httpRequest(FACE_CREATE_PERSON_URL + StringRoleUtil.joinString(params), "GET", null);
            if (resultJson.getInt("added_face") > 0) {
                FacePerson facePerson = new FacePerson();
                facePerson.setPersonId(resultJson.getString("person_id"));
                facePerson.setPersonName(resultJson.getString("person_name"));
                facePerson.setPersonTag(resultJson.getString("tag"));
                facePerson.setPersonDate(DateUtil.getCurrentDate());
                facePersonDao.insert(facePerson);
                return true;
            } else {
                return false;
            }

        } catch (Exception e) {
            return false;
        }
    }


    /**
     * 新建Face
     * @param faceName
     * @param faceId
     * @param tag
     *
     * @return face_id
     * **/
    private final static String FACE_CREATE_FACESET_URL = BaseUtil.getFaceApiUrl() + "/faceset/create";
    public String createFace(String faceName, String[] faceId, String tag) {
        String result = "error";
        List<String> params = new ArrayList<>();

        if (faceName != null)   params.add("faceset_name=" + faceName);
        if (faceId != null) params.add("face_id=" + StringRoleUtil.addArray(faceId));
        if (tag != null)    params.add("tag" + tag);

        try {
            JSONObject resultJson = HttpUtil.httpRequest(FACE_CREATE_FACESET_URL + StringRoleUtil.joinString(params), "GET", null);
            if (resultJson.getInt("added_face") > 0) {
                result = resultJson.getString("faceset_id");
                FaceFaceset faceFaceset = new FaceFaceset(result, resultJson.getString("faceset_name"),
                        resultJson.getString("tag"), DateUtil.getCurrentDate());
                faceFacesetDao.insert(faceFaceset);
            }
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
        } finally {
            return result;
        }
    }

}
